import { ColumnConfiguration, SeedConfiguration } from "@/views/dev-types"
import { snakeCase } from 'lodash-es'

export function getNameColumn(seed: SeedConfiguration) {
  const nameColumn = seed.nameColumn
  const columns = seed.columns || {}
  if(nameColumn) {
    if(columns[snakeCase(nameColumn)]) return nameColumn

    throw Error(`【${seed.name}】下的【nameColumn】【${nameColumn}】不存在`)
  }

  if(columns['name']) return 'name'

  throw Error(`请为【${seed.name}】配置【nameColumn】`)
}

export function getRefColumns(seed: SeedConfiguration) {
  const ret: Record<string, ColumnConfiguration> = {}
  if(!seed) return ret
  
  const columns = seed.columns
  for(const columnName in columns) {
    const column = columns[columnName]
    if(column.type === 'ref') ret[columnName] = column
  }

  return ret
}